{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a52400c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n",
      "{1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "#声明一个集合\n",
    "set1 = {1,2,3,4,1,2,3,4}\n",
    "\n",
    "print(type(set1))\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "efc5461b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3}\n",
      "{1, 2, 3, 4, 'a', 'c', 'b'}\n",
      "{4}\n",
      "{'b', 'a', 'c'}\n"
     ]
    }
   ],
   "source": [
    "set2 = {'a', 'b', 'c', 1, 2, 3}\n",
    "\n",
    "#求交集\n",
    "print(set1 & set2)\n",
    "\n",
    "#求并集\n",
    "print(set1 | set2)\n",
    "\n",
    "#求差集\n",
    "print(set1 - set2)\n",
    "print(set2 - set1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "42e780a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5}\n",
      "{1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "#集合增删\n",
    "set1.add(5)\n",
    "print(set1)\n",
    "\n",
    "set1.discard(5)\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1e78934a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({1, 2, 3, 4})\n",
      "<class 'frozenset'>\n",
      "frozenset({1, 2, 3})\n",
      "frozenset({1, 2, 3, 4, 'a', 'c', 'b'})\n",
      "frozenset({4})\n",
      "frozenset({'b', 'a', 'c'})\n"
     ]
    }
   ],
   "source": [
    "#冻结集合 frozenset 一种不可变的set\n",
    "set1 = frozenset(set1)\n",
    "set2 = frozenset(set2)\n",
    "print(set1)\n",
    "print(type(set1))\n",
    "\n",
    "#求交集\n",
    "print(set1 & set2)\n",
    "\n",
    "#求并集\n",
    "print(set1 | set2)\n",
    "\n",
    "#求差集\n",
    "print(set1 - set2)\n",
    "print(set2 - set1)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
